package com.tony.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tony.business.service.AdminUserService;
import com.tony.domain.bo.JsonResult;
import com.tony.domain.dto.AdminUserDTO;
import com.tony.domain.dto.SelectAdminUserPageDTO;
import com.tony.domain.enumerate.EnableEnum;
import com.tony.domain.vo.AdminUserPageVO;
import com.tony.domain.vo.AdminUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author tony
 * @date 2020/5/30 17:49
 */
@Api(tags = "用户相关")
@RestController
@RequestMapping("/adminUser")
public class AdminUserController extends BaseController {

    @Autowired
    private AdminUserService adminUserService;

    AdminUserController() {
        super();
    }

    @PostMapping
    @ApiOperation(value = "添加用户", httpMethod = "POST")
    public JsonResult<AdminUserVO> add(@Validated AdminUserDTO dto) {
        return adminUserService.add(dto, getUserInfo());
    }

    @PutMapping
    @ApiOperation(value = "修改用户", httpMethod = "PUT")
    public JsonResult<AdminUserVO> update(@Validated AdminUserDTO dto) {
        return adminUserService.update(dto, getUserInfo());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户", httpMethod = "DELETE")
    public JsonResult<Boolean> delete(@PathVariable("id") Long id) {
        return new JsonResult<>(adminUserService.removeById(id));
    }

    @GetMapping
    @ApiOperation(value = "查询用户分页", httpMethod = "GET")
    public JsonResult<Page<AdminUserPageVO>> page(@Validated SelectAdminUserPageDTO pageDTO) {
        return adminUserService.selectAdminUserPage(pageDTO);
    }

    @PutMapping("/{id}/{enable}")
    @ApiOperation(value = "启用/禁用用户", httpMethod = "PUT")
    public JsonResult<Boolean> enable(@PathVariable("id") Long id, @PathVariable("enable") EnableEnum enable) {
        return adminUserService.enable(id, enable, getUserInfo());
    }
}
